home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
4_6.lha
/
4_6
/
4_6a.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
1KB
|
64 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ quick sort
/ Version 1
/ Based on "Algorithms" by Robert Sedgewick
/ Chapter 9, pp 103-113
include <swap.h>
/ Global variables
tatic int *x;
/ partition the array into two halves
/ after the partition,
/ K[left] < ... < K[i] < ... < K[right]
/ iqsort will then recursively sort
/ K[left] through K[i-1] and
/ K[i+1] through K[right]
tatic int partition(int left, int right)
int i = left - 1;
int j = right;
int v = x[j];
do {
do {
i++;
} while (x[i] < v);
do {
j--;
} while (j > left && x[j] > v);
swap(x[i], x[j]);
} while (i < j);
swap(x[j], x[i]);
swap(x[i], x[right]);
return i;
/ the secondary routine which will
/ do the actual sorting
tatic void iqsort(int left, int right)
if (right > left)
{
int i = partition(left, right);
iqsort(left, i-1);
iqsort(i+1, right);
}
/ external entry point
oid qsort1(void *array, unsigned nel,
unsigned, int (*)(const void*,const void*))
x = (int*)array; // save global pointer
iqsort(0, nel-1);